package controller;

import java.util.Collection;

import model.ContractRule;
import model.LeadRule;
import model.OpportunityRule;
import model.Rule;
import util.SMSUtil;
import Repository.Repository;
import Repository.RepositoryFactory;
import constants.SMSConstants;

public class RuleController extends Controller {
	
	String request;


	public RuleController(String requestType) {
		request = requestType.trim().toLowerCase();
	}

	public void handleRequest() {
		try{
			if (request.contains("create"))
				insert();
			else if (request.contains("delete"))
				delete();
			else if (request.contains("update"))
				update();
		}catch(NullPointerException ne){
			System.out.println("***Rule does not exist***");
			ne.printStackTrace();
		}catch(NumberFormatException ne){
			System.out.println("***Incorrect format***");
		}catch(Exception e){
			System.out.println("Error!!");
		}

	}

	public void insert() {
	     String  ruledescription, rulename, ruleType;
		String choice;
		Rule rule = null;
		System.out.println("Processing in Rule Controller:......");
		
		do{
			Repository repository = RepositoryFactory
					.getRepository(SMSConstants.RULE);
			
			System.out.println("Enter the Type (Lead/Opportunity/Contract) ");
			ruleType = SMSUtil.getUserInput();
			System.out.println("Enter Rule Name:");
			rulename = SMSUtil.getUserInput();
			System.out.println("Enter Rule Description :");
			ruledescription = SMSUtil.getUserInput();
			
				
			if (ruleType.equalsIgnoreCase("Lead")) {
				rule = new LeadRule();
			}
	
			else if (ruleType.equalsIgnoreCase("Opportunity")) {
	
				rule = new OpportunityRule();
	
			} else if (ruleType.equalsIgnoreCase("Contract")) {
				rule = new ContractRule();
	
			}else{
				System.out.println("Invalid Rule type!!");
			}
	
			long id = Rule.nextId();
			rule.setRuleDescripton(ruledescription);
			rule.setRuleName(rulename);
			rule.setRuleType(ruleType);
			rule.setRuleID(id);
			repository.insert(id, rule);
			System.out.println("Successfully created rule.");
			
			System.out.println("Do you wish to create more rules?(Y/N)");
			choice = SMSUtil.getUserInput();

		} while (choice.toLowerCase().trim().equals("y"));
		
	}

	public void delete() {
		
		
		String choice;
		Repository rulerep = RepositoryFactory.getRepository(SMSConstants.RULE);
		do{
			
			// fetches all the rules from rule repository
			Collection lstRule = rulerep.retrieveAll();
			SMSUtil.displayRuleHeader();
			for (Object rule : lstRule) {
				SMSUtil.formatRule((Rule)rule);
			}
			SMSUtil.displayEnd();
			System.out.println("Enter id of the Rule you want to delete :");
			String id = SMSUtil.getUserInput();
			
			rulerep.delete(Long.parseLong(id));
			System.out.println("Successfully deleted rule.");
			System.out.println("Do you wish to delete more rules?(Y/N)");
			choice = SMSUtil.getUserInput();
	
		} while (choice.toLowerCase().trim().equals("y"));
	}

	public void update() {
		String  ruledescription, rulename, ruleType;
		Repository rulerep = RepositoryFactory.getRepository(SMSConstants.RULE);
		String choice;
		do{
			// fetches all the rules from rule repository
			Collection lstRule = rulerep.retrieveAll();
			
			SMSUtil.displayRuleHeader();
			for (Object rule : lstRule) {
				SMSUtil.formatRule((Rule)rule);
			}
			SMSUtil.displayEnd();
			// fetches specific data as entered by the user
			System.out.println("Enter id of the Rule you want to update :");
			String idUpdate = SMSUtil.getUserInput();
						
			// asks the user to enter data so as it can be updated
			System.out.println("Enter Rule Name:");
			rulename = SMSUtil.getUserInput();
			System.out.println("Enter Rule Description:");
			ruledescription = SMSUtil.getUserInput();
			
			Rule rule = (Rule)rulerep.retrieve(Long.parseLong(idUpdate));
			//rule.setRuleID(Long.parseLong(idUpdate));
			rule.setRuleDescripton(ruledescription);
			rule.setRuleName(rulename);
		//	rule.setRuleType(ruleType);
			rulerep.update(Long.parseLong(idUpdate), rule);
			System.out.println("Successfully updated rule.");
			System.out.println("Do you wish to update more rules?(Y/N)");
			choice = SMSUtil.getUserInput();
	
		} while (choice.toLowerCase().trim().equals("y"));

	}

}
